﻿using IVBlog.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IVBlog.Controllers
{
    public class PersonalController : Controller
    {
        private readonly IVBlogContext iVBlogContext;
        public PersonalController(IVBlogContext iVBlog)
        {
            iVBlogContext = iVBlog;
        }

        //导航页
        public IActionResult Index()
        {
            try
            {
                return View();
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        public bool isLogin()
        {
            if (HttpContext.Session.GetInt32("adminID") == 0 || HttpContext.Session.GetInt32("adminID") == null)
            {
                return false;
            }
            return true;
        }

        //登录页
        public IActionResult Login()
        {
            try
            {
                if (!isLogin())
                {
                    return View();
                }
                else
                {
                    return RedirectToAction("Index", "Back");
                }
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [HttpPost]
        public IActionResult Login(Admin user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Admin admin = iVBlogContext.Admin.FirstOrDefault(t => t.Aaccount == user.Aaccount && t.Apassword == user.Apassword);
                    if (admin != null)
                    {
                        HttpContext.Session.SetInt32("adminID", admin.AdminId);
                        return RedirectToAction("Index", "Back");
                    }
                    TempData["Error"] = "账号或密码错误!";
                }
                return View();
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        //登出
        public IActionResult SignOut()
        {
            try
            {
                HttpContext.Session.SetInt32("adminID", 0);
                return RedirectToAction("Login");
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        //信息页
        public IActionResult Info()
        {
            try
            {
                Info info = iVBlogContext.Info.FirstOrDefault();
                ViewBag.info = info;
                return View();
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        //修改 EditInfo
        public IActionResult EditInfo(int id, string InfoName, string Motto, string Email, string Qqgroup)
        {
            try
            {
                Info info = iVBlogContext.Info.FirstOrDefault(t => t.InfoId == id);
                info.InfoName = InfoName;
                info.Motto = Motto;
                info.Email = Email;
                info.Qqgroup = Qqgroup;
                iVBlogContext.SaveChanges();
                return RedirectToAction("Info");
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }


    }
}
